import {sha1} from '../../lib/hash'
import {randomString} from '../../lib/random'

export function hashMask(str: string, salt: string): string {
  return sha1(sha1(str + salt) + salt);
}

export function checkHashMasked(str: string, salt: string, masked: string): boolean {
  return hashMask(str, salt) === masked;
}

export function encrypt(realPassword: string): string {
    const salt = randomString(17);
    return hashMask(realPassword, salt) + '.' + salt;
}

export function checkEncrypted(realPassword: string, encryptedPassword: string): boolean {
    const [masked, salt] = encryptedPassword.split('.')
    return hashMask(realPassword, salt) === masked;
}